<!--
	作者：956911785@qq.com
	时间：2019年8月1日17:44:32
	描述：书籍分类-添加
-->

<template>
	<Modal :title="title" v-model="modal0" width="500px">
		<Form ref="params" :model="params" :rules="ruleValidate" :label-width="120" label-position="left">
			<div style="width:100%;height:300px; overflow-y: scroll;">
		      	
				<div style="width:100%;height: 60px;line-height: 60px;float:left;text-align: left;">
			        <FormItem  prop="spname" label="商品名称">
			        	<Input v-model="params.spname" placeholder="请输入商品名称" clearable style="width: 90%"></Input>
	        		</FormItem>
		      	</div>
		      	
		      	<div style="width:100%;height: 60px;line-height: 60px;float:left;text-align: left;">
			        <FormItem  prop="allnum" label="商品总数量">
			        	<Input v-model="params.allnum" placeholder="请输入商品总数量" clearable style="width: 90%"></Input>
	        		</FormItem>
		      	</div>
		      	
		      	<div style="width:100%;height: 60px;line-height: 60px;float:left;text-align: left;">
			        <FormItem  prop="needjf" label="所需积分">
			        	<Input v-model="params.needjf" placeholder="请输入所需积分" clearable style="width: 90%"></Input>
	        		</FormItem>
		      	</div>
		      	
		        <div style="width:100%;height: 60px;line-height: 60px;float:left;text-align: left;">
				    <div style="float:left;">
					    <FormItem label="商品图片">
						    <Upload
						        ref="upload"
						        :show-upload-list="false"
						        :on-success="handleSuccess"
						        :format="['jpg','jpeg','png']"
						        :max-size="2048"
						        :on-format-error="handleFormatError"
						        :on-exceeded-size="handleMaxSize"
						        type="drag"
						        :action="action"
						        style="display: inline-block;width:80px;">
						        <div style="width: 80px;height:80px;line-height: 80px;">
						            <Icon type="ios-camera" size="20"></Icon>
						        </div>
						    </Upload>
					    </FormItem>
				    </div>
				   
				    <div style="width:120px;float:left;">
				        <template v-if="imgurl">
				            <img :src="imgurl" @click="handleView()" style="width:120px;height:auto;float:left;">
				            <div style="width:120px;float:left;text-align: center;">
				                <Icon type="ios-trash-outline" @click.native="handleRemove()" style="font-size:15px;"></Icon>
				            </div>
				        </template>
				    </div>
				    <Modal title="图片浏览" v-model="visible">
				        <img :src="imgurl" v-if="visible" style="width: 100%">
				    </Modal>
		      	</div>
				
		   </div> 
	    </Form>
	   	<div slot="footer">
	    	<Button type="text"  @click="closeModal()">取消</Button>
	    	<Button type="primary" @click="addsave()" >确定</Button>
	    </div>
     </Modal>
</template>

<script>
	import _config from "../../../../config/config"
	export default {
		name:"tconent",
		props: {
    		teas:{
    			type:Object,	
    			default: function() {
					return null
				}
    		},
    		title:{
    			type:String,	
    		},
    	},
		data() {
			//长度控制
			const lenPass = (rule, str, callback) => {
			  if(str.length>rule.length){
			  	 callback(new Error('字符串长度在1-'+rule.length+'之间！'));
			  }
			  return callback();
			};
			
			//正整数控制
			const zzsPass1 = (rule, value, callback) => {
			    let reg = /^\+?[1-9][0-9]*$/; 
				if(!reg.test(value)){
				    callback(new Error('请输入正整数！'));
				}else{
				  	return callback();
				}
			};
			
			return {
				ruleValidate: {
		            spname: [
		                {required: true, message: '请输入商品名称！', trigger: 'blur'},
		                {length:30,validator:lenPass,trigger:'blur'}
		            ],
		            allnum: [
		                {required: true, message: '请输入商品总数量！', trigger: 'blur'},
		                {validator:zzsPass1,trigger:'blur'}
		            ],
		            needjf: [
		                {required: true, message: '请输入所需积分！', trigger: 'blur'},
		                {validator:zzsPass1,trigger:'blur'}
		            ],
		        },   
                modal0:false,
                params:{
                	spid:"", 
					spname:"", 
					allnum:"", 
					needjf:"", 
					spurl:"", 
					ydh:"", 
                },
          		action:_config._ADMIN_SERVER+'/readbook/filesUpload.html',
          		
          		imgurl: '',
                visible: false,
                
			} 
		},
		methods: {
			addsave:function(){
				this.$refs.params.validate((valid) => {
					if (valid){
						const params={...this.params};
						this.$emit("addsave",params);
                    }
               })
	        }, 
            closeModal(){
	      	    this.modal0 = false;
	      	    this.resetpar();
	        },
	        resetpar(){
	        	this.params={
	        		spid:"", 
					spname:"", 
					allnum:"", 
					needjf:"", 
					spurl:"", 
					ydh:"", 
				};
	        },
	      
	      
	     
			handleView () {
                this.visible = true;
            },
            handleRemove (file) {
				this.imgurl="";
				this.params.spurl="";
            },
            handleSuccess (res, file) {
                this.imgurl=_config._ADMIN_SERVER+"/readbook/"+res[0].FILE_PATH;
                this.params.spurl=res[0].FILE_PATH;
            },
            handleFormatError (file) {
                this.$Notice.warning({
                    title: '文件上传失败',
                    desc: '文件' + file.name + '无法上传, 上传文件请选择jpg or png.'
                });
            },
            handleMaxSize (file) {
                this.$Notice.warning({
                    title: '提示',
                    desc: '文件  ' + file.name + ' 太大, 上传的文件不能超过2M.'
                });
            },
            
		},
		mounted() {
		},
		watch:{
			modal0(news,olds){
				if(!news){
					 this.resetpar();
					 this.imgurl="";
				}else{
					this.params={...this.teas};
					if(this.params.spurl){
						this.imgurl=_config._ADMIN_SERVER+"/readbook/"+this.params.spurl;
					}
				}
			}
		},
		computed: {
		
		}
		
	}
</script>

<style scoped="scoped">
	
</style>



